// pages/content/kucun.js
var app = getApp();
var api = require('../../utils/api.js');
var rsa = require('../../utils/rsa.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    search: {
      searchValue: '',
      showClearBtn: false
    },
    selectpaix: 0,
    inputValue: '',
    money: 0,
    goods_amount: 0,
    goodArr: [],
    noword: false,
    pageNum:1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.culateStore();
    this.responseServer();
  },
  searchAction: function () {
    this.setData({
      goodArr:[],
      pageNum:1,
      money: 0,
      goods_amount: 0,
    })
    this.querygood();
  },
  querygood:function(){
    const shopnum = app.globalData.shoopnum;
    const setion = app.globalData.session;
    rsa.Cpic_fetchPost(rsa.SERVERPORT, {
      'method': 'goods.keyword', "seller_bn": shopnum, 'session_id': setion, 'keyword': this.data.inputValue
    }, (err, res) => {
      console.log(res);
      var datas = res.data.list;
      if(datas.length ==0 ){
        wx.showToast({
          title: '未查询到商品',
          icon:'none',
          duration: 2000
        })
        return;
      }
      var tempArr = [];
      var total = 0;
      var money = 0;
      for (var i = 0; i < datas.length; i++) {
        var templist = datas[i];
         var gname = templist.name;
        var numbe = templist.quantity;
         if (!numbe || numbe == '') {
           numbe = '0'
          }
          if (!gname) continue;
         var pri = templist.amount;
          total +=parseInt(numbe);
          money +=  parseFloat(pri);
          
          var sudic = { name: gname, num: numbe, money: pri };
          tempArr.push(sudic);
        }
      this.setData({
        goodArr: tempArr,
        money: money,
        goods_amount: total,
      })
      
    })
  },


  //查询库存
  culateStore:function(){
    const shopnum = app.globalData.shoopnum;
    const setion = app.globalData.session;
    rsa.Cpic_fetchPost(rsa.SERVERPORT, {
      'method': 'store.goodscount', "seller_bn": shopnum, 'session_id': setion
    }, (err, res) => {
      console.log(res);
      if (!res.data || res.data!=null) {
        this.setData({
          money: res.data.total_amount,
          goods_amount: res.data.quantity,
        })
      } else {
        wx.showToast({
          title: err,
          icon: 'fail',
          duration: 2000
        })
      }
    })
  },
  //向库存查询
  responseServer: function () {
    const shopnum = app.globalData.shoopnum;
    const dtype = this.data.selectpaix;
    const pagenumber = this.data.pageNum;
    var ody = 'qdesc';
    switch (dtype) {
      case '0':
        ody = 'qdesc';
        break;
      case '1':
        ody = 'adesc';
        break;
    }
    const inputvalue = this.data.inputValue;
    const setion = app.globalData.session;
    rsa.Cpic_fetchPost(rsa.SERVERPORT, {'method': 'goods.search', "seller_bn": shopnum, 'session_id': setion, 'cat_id': '','orderby':ody,'barcode': '', "name": inputvalue, "syns_time": '', "page_num": '10', "page": pagenumber
    }, (err, res) => {
      if(!res||res=='' ){
        return;
      }
      var listArr = res.data.list;
      var tpage =parseInt(res.data.total_page);
      if(pagenumber>=tpage){
        this.setData({
          noword:false
        })
      }else{
        this.setData({
          noword: true,
        })
      }
      if (listArr.length > 0) {
        var tempArr = this.data.goodArr;
        for(var i=0;i<listArr.length;i++){
          var templist = listArr[i];
          var gname = templist.name;
          var numbe = templist.quantity;
          if(!numbe||numbe==''){
            numbe ='0'
          }
          if(!gname)continue;
          var pri = templist.amount  ;
          var sudic = { name: gname, num: numbe, money: pri};
          tempArr.push(sudic);
        }
        this.setData({
          goodArr: tempArr,
          money: res.data.total_amount,
          goods_amount: res.data.total_count,
        })
      } else {
        wx.showToast({
          title: err,
          icon: 'fail',
          duration: 2000
        })
      }
    })
  },
  //上拉
  // 下拉刷新  
  onPullDownRefresh: function () {
    // // 显示导航栏loading  
    // wx.showNavigationBarLoading();
    // // 调用接口加载数据  
    // this.loadData();
    // // 隐藏导航栏loading  
    // wx.hideNavigationBarLoading();
    // // 当处理完数据刷新后，wx.stopPullDownRefresh可以停止当前页面的下拉刷新  
    wx.stopPullDownRefresh();
  },
  // 上拉加载  
  onReachBottom(e) {
    let  that = this;
    if (that.data.noword) {
      that.setData({
        pageNum: that.data.pageNum + 1,  // 每次触发上拉事件，把pageNum+1  
      });

      that.responseServer();
    }
  },

  tvpaction: function (e) {
    const kc = e.currentTarget.dataset.info;
    this.setData({
      selectpaix: kc,
      pageNum: 1,  // 每次触发上拉事件，把pageNum+1  
      goodArr:[],
    })
    this.responseServer();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  bindKeyInput: function (e) {
    this.setData({
      inputValue: e.detail.value
    })
  },

  //输入内容时
  searchActiveChangeinput: function (e) {
    const val = e.detail.value;
    this.setData({
      'search.showClearBtn': val != '' ? true : false,
      'search.searchValue': val
    })
  },
  //点击清除搜索内容
  searchActiveChangeclear: function (e) {
    this.setData({
      'search.showClearBtn': false,
      'search.searchValue': ''
    })
  },
  //点击聚集时
  focusSearch: function () {
    if (this.data.search.searchValue) {
      this.setData({
        'search.showClearBtn': true
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})